import _defineProperty from "@babel/runtime/helpers/esm/defineProperty";
import _regeneratorRuntime from "@babel/runtime/regenerator";
import _objectSpread from "@babel/runtime/helpers/esm/objectSpread2";

/**
 * @date 2018-09-01
 */
// import { isEmpty } from 'lodash';
import { createPagination, getResponse, getCurrentOrganizationId, isTenantRoleLevel } from 'utils/utils';
import { queryRangeList as _queryRangeList, queryOrganizationRangeList, queryRuleList as _queryRuleList, queryOrganizationRuleList, queryCode as _queryCode, createRule as _createRule, createOrganizationRule, createRange as _createRange, createOrganizationRange, updateRule as _updateRule, updateOrganizationRule, updateRange as _updateRange, updateOrganizationRange, deleteRule as _deleteRule, deleteOrganizationRule, deleteRange as _deleteRange, deleteOrganizationRange, addPermissionRel as _addPermissionRel, queryPermissionRel as _queryPermissionRel, deletePermissionRel as _deletePermissionRel } from "../services/permissionService";
export default {
  namespace: 'permission',
  state: {
    code: {},
    range: {
      list: {
        dataSource: [],
        pagination: {
          pageSize: 10,
          total: 0,
          current: 1
        }
      }
    },
    rule: {
      list: {
        dataSource: [],
        pagination: {
          pageSize: 10,
          total: 0,
          current: 1
        }
      }
    },
    rangeServiceNameExclList: [],
    rangeSqlidExclList: [],
    rangeTenantExclList: []
  },
  effects: {
    queryRangeList: /*#__PURE__*/_regeneratorRuntime.mark(function queryRangeList(_ref, _ref2) {
      var payload, put, call, organizationId, res, response;
      return _regeneratorRuntime.wrap(function queryRangeList$(_context) {
        while (1) {
          switch (_context.prev = _context.next) {
            case 0:
              payload = _ref.payload;
              put = _ref2.put, call = _ref2.call;
              organizationId = getCurrentOrganizationId();

              if (!isTenantRoleLevel()) {
                _context.next = 9;
                break;
              }

              _context.next = 6;
              return call(queryOrganizationRangeList, _objectSpread({
                page: 0,
                size: 10
              }, payload), organizationId);

            case 6:
              res = _context.sent;
              _context.next = 12;
              break;

            case 9:
              _context.next = 11;
              return call(_queryRangeList, _objectSpread({
                page: 0,
                size: 10
              }, payload));

            case 11:
              res = _context.sent;

            case 12:
              response = getResponse(res);

              if (!response) {
                _context.next = 16;
                break;
              }

              _context.next = 16;
              return put({
                type: 'updateRangeStateReducer',
                payload: {
                  list: {
                    dataSource: response.content || [],
                    pagination: createPagination(response)
                  }
                }
              });

            case 16:
            case "end":
              return _context.stop();
          }
        }
      }, queryRangeList);
    }),
    queryRuleList: /*#__PURE__*/_regeneratorRuntime.mark(function queryRuleList(_ref3, _ref4) {
      var payload, put, call, organizationId, res, response;
      return _regeneratorRuntime.wrap(function queryRuleList$(_context2) {
        while (1) {
          switch (_context2.prev = _context2.next) {
            case 0:
              payload = _ref3.payload;
              put = _ref4.put, call = _ref4.call;
              organizationId = getCurrentOrganizationId();

              if (!isTenantRoleLevel()) {
                _context2.next = 9;
                break;
              }

              _context2.next = 6;
              return call(queryOrganizationRuleList, _objectSpread({
                page: 0,
                size: 10
              }, payload), organizationId);

            case 6:
              res = _context2.sent;
              _context2.next = 12;
              break;

            case 9:
              _context2.next = 11;
              return call(_queryRuleList, _objectSpread({
                page: 0,
                size: 10
              }, payload));

            case 11:
              res = _context2.sent;

            case 12:
              response = getResponse(res);

              if (!response) {
                _context2.next = 16;
                break;
              }

              _context2.next = 16;
              return put({
                type: 'updateRuleStateReducer',
                payload: {
                  list: {
                    dataSource: response.content || [],
                    pagination: createPagination(response)
                  }
                }
              });

            case 16:
            case "end":
              return _context2.stop();
          }
        }
      }, queryRuleList);
    }),
    queryCode: /*#__PURE__*/_regeneratorRuntime.mark(function queryCode(_ref5, _ref6) {
      var payload, put, call, res, response;
      return _regeneratorRuntime.wrap(function queryCode$(_context3) {
        while (1) {
          switch (_context3.prev = _context3.next) {
            case 0:
              payload = _ref5.payload;
              put = _ref6.put, call = _ref6.call;
              _context3.next = 4;
              return call(_queryCode, payload);

            case 4:
              res = _context3.sent;
              response = getResponse(res);

              if (!response) {
                _context3.next = 9;
                break;
              }

              _context3.next = 9;
              return put({
                type: 'setCodeReducer',
                payload: _defineProperty({}, payload.lovCode, response)
              });

            case 9:
            case "end":
              return _context3.stop();
          }
        }
      }, queryCode);
    }),
    createRule: /*#__PURE__*/_regeneratorRuntime.mark(function createRule(_ref7, _ref8) {
      var data, call, organizationId, res;
      return _regeneratorRuntime.wrap(function createRule$(_context4) {
        while (1) {
          switch (_context4.prev = _context4.next) {
            case 0:
              data = _ref7.data;
              call = _ref8.call;
              organizationId = getCurrentOrganizationId();

              if (!isTenantRoleLevel()) {
                _context4.next = 9;
                break;
              }

              _context4.next = 6;
              return call(createOrganizationRule, data, organizationId);

            case 6:
              res = _context4.sent;
              _context4.next = 12;
              break;

            case 9:
              _context4.next = 11;
              return call(_createRule, data, organizationId);

            case 11:
              res = _context4.sent;

            case 12:
              return _context4.abrupt("return", getResponse(res));

            case 13:
            case "end":
              return _context4.stop();
          }
        }
      }, createRule);
    }),
    updateRule: /*#__PURE__*/_regeneratorRuntime.mark(function updateRule(_ref9, _ref10) {
      var data, call, organizationId, res;
      return _regeneratorRuntime.wrap(function updateRule$(_context5) {
        while (1) {
          switch (_context5.prev = _context5.next) {
            case 0:
              data = _ref9.data;
              call = _ref10.call;
              organizationId = getCurrentOrganizationId();

              if (!isTenantRoleLevel()) {
                _context5.next = 9;
                break;
              }

              _context5.next = 6;
              return call(updateOrganizationRule, data, organizationId);

            case 6:
              res = _context5.sent;
              _context5.next = 12;
              break;

            case 9:
              _context5.next = 11;
              return call(_updateRule, data);

            case 11:
              res = _context5.sent;

            case 12:
              return _context5.abrupt("return", getResponse(res));

            case 13:
            case "end":
              return _context5.stop();
          }
        }
      }, updateRule);
    }),
    createRange: /*#__PURE__*/_regeneratorRuntime.mark(function createRange(_ref11, _ref12) {
      var data, call, organizationId, res;
      return _regeneratorRuntime.wrap(function createRange$(_context6) {
        while (1) {
          switch (_context6.prev = _context6.next) {
            case 0:
              data = _ref11.data;
              call = _ref12.call;
              organizationId = getCurrentOrganizationId();

              if (!isTenantRoleLevel()) {
                _context6.next = 9;
                break;
              }

              _context6.next = 6;
              return call(createOrganizationRange, data, organizationId);

            case 6:
              res = _context6.sent;
              _context6.next = 12;
              break;

            case 9:
              _context6.next = 11;
              return call(_createRange, data);

            case 11:
              res = _context6.sent;

            case 12:
              return _context6.abrupt("return", getResponse(res));

            case 13:
            case "end":
              return _context6.stop();
          }
        }
      }, createRange);
    }),
    updateRange: /*#__PURE__*/_regeneratorRuntime.mark(function updateRange(_ref13, _ref14) {
      var data, call, organizationId, res;
      return _regeneratorRuntime.wrap(function updateRange$(_context7) {
        while (1) {
          switch (_context7.prev = _context7.next) {
            case 0:
              data = _ref13.data;
              call = _ref14.call;
              organizationId = getCurrentOrganizationId();

              if (!isTenantRoleLevel()) {
                _context7.next = 9;
                break;
              }

              _context7.next = 6;
              return call(updateOrganizationRange, data, organizationId);

            case 6:
              res = _context7.sent;
              _context7.next = 12;
              break;

            case 9:
              _context7.next = 11;
              return call(_updateRange, data);

            case 11:
              res = _context7.sent;

            case 12:
              return _context7.abrupt("return", getResponse(res));

            case 13:
            case "end":
              return _context7.stop();
          }
        }
      }, updateRange);
    }),
    deleteRule: /*#__PURE__*/_regeneratorRuntime.mark(function deleteRule(_ref15, _ref16) {
      var payload, call, organizationId, res;
      return _regeneratorRuntime.wrap(function deleteRule$(_context8) {
        while (1) {
          switch (_context8.prev = _context8.next) {
            case 0:
              payload = _ref15.payload;
              call = _ref16.call;
              organizationId = getCurrentOrganizationId();

              if (!isTenantRoleLevel()) {
                _context8.next = 9;
                break;
              }

              _context8.next = 6;
              return call(deleteOrganizationRule, payload, organizationId);

            case 6:
              res = _context8.sent;
              _context8.next = 12;
              break;

            case 9:
              _context8.next = 11;
              return call(_deleteRule, payload);

            case 11:
              res = _context8.sent;

            case 12:
              return _context8.abrupt("return", getResponse(res));

            case 13:
            case "end":
              return _context8.stop();
          }
        }
      }, deleteRule);
    }),
    deleteRange: /*#__PURE__*/_regeneratorRuntime.mark(function deleteRange(_ref17, _ref18) {
      var payload, call, organizationId, res;
      return _regeneratorRuntime.wrap(function deleteRange$(_context9) {
        while (1) {
          switch (_context9.prev = _context9.next) {
            case 0:
              payload = _ref17.payload;
              call = _ref18.call;
              organizationId = getCurrentOrganizationId();

              if (!isTenantRoleLevel()) {
                _context9.next = 9;
                break;
              }

              _context9.next = 6;
              return call(deleteOrganizationRange, payload, organizationId);

            case 6:
              res = _context9.sent;
              _context9.next = 12;
              break;

            case 9:
              _context9.next = 11;
              return call(_deleteRange, payload);

            case 11:
              res = _context9.sent;

            case 12:
              return _context9.abrupt("return", getResponse(res));

            case 13:
            case "end":
              return _context9.stop();
          }
        }
      }, deleteRange);
    }),
    addPermissionRel: /*#__PURE__*/_regeneratorRuntime.mark(function addPermissionRel(_ref19, _ref20) {
      var data, call, res;
      return _regeneratorRuntime.wrap(function addPermissionRel$(_context10) {
        while (1) {
          switch (_context10.prev = _context10.next) {
            case 0:
              data = _ref19.data;
              call = _ref20.call;
              _context10.next = 4;
              return call(_addPermissionRel, data);

            case 4:
              res = _context10.sent;
              return _context10.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context10.stop();
          }
        }
      }, addPermissionRel);
    }),
    queryPermissionRel: /*#__PURE__*/_regeneratorRuntime.mark(function queryPermissionRel(_ref21, _ref22) {
      var rangeId, call, res;
      return _regeneratorRuntime.wrap(function queryPermissionRel$(_context11) {
        while (1) {
          switch (_context11.prev = _context11.next) {
            case 0:
              rangeId = _ref21.rangeId;
              call = _ref22.call;
              _context11.next = 4;
              return call(_queryPermissionRel, rangeId);

            case 4:
              res = _context11.sent;
              return _context11.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context11.stop();
          }
        }
      }, queryPermissionRel);
    }),
    deletePermissionRel: /*#__PURE__*/_regeneratorRuntime.mark(function deletePermissionRel(_ref23, _ref24) {
      var payload, call, res;
      return _regeneratorRuntime.wrap(function deletePermissionRel$(_context12) {
        while (1) {
          switch (_context12.prev = _context12.next) {
            case 0:
              payload = _ref23.payload;
              call = _ref24.call;
              _context12.next = 4;
              return call(_deletePermissionRel, payload);

            case 4:
              res = _context12.sent;
              return _context12.abrupt("return", getResponse(res));

            case 6:
            case "end":
              return _context12.stop();
          }
        }
      }, deletePermissionRel);
    })
  },
  reducers: {
    updateState: function updateState(state, _ref25) {
      var payload = _ref25.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    updateStateReducer: function updateStateReducer(state, _ref26) {
      var payload = _ref26.payload;
      return _objectSpread(_objectSpread({}, state), payload);
    },
    updateRangeStateReducer: function updateRangeStateReducer(state, _ref27) {
      var payload = _ref27.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        range: _objectSpread(_objectSpread({}, state.range), payload)
      });
    },
    updateRuleStateReducer: function updateRuleStateReducer(state, _ref28) {
      var payload = _ref28.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        rule: _objectSpread(_objectSpread({}, state.rule), payload)
      });
    },
    setCodeReducer: function setCodeReducer(state, _ref29) {
      var payload = _ref29.payload;
      return _objectSpread(_objectSpread({}, state), {}, {
        code: Object.assign(state.code, payload)
      });
    }
  }
};